package com.du.integration.server.handle;


import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Desc :
 * @Author: shuangdu
 * @date : 2024/1/9
 */
public class MqttMsgHandleFactory {

    private static Map<String, MqttMsgHandle> handleMap = new ConcurrentHashMap<>();

    public static void registerHandle(String topic, MqttMsgHandle handle) {
        handleMap.put(topic, handle);
    }

    public static MqttMsgHandle getHandle(String topic) {
        Set<Map.Entry<String, MqttMsgHandle>> entries = handleMap.entrySet();
        for (Map.Entry<String, MqttMsgHandle> next : entries) {
            if (next.getValue().filter(topic)) {
                return next.getValue();
            }
        }
        return new DefaultMqttMsgHandle();
    }

}
